#!/usr/bin/env python3
import tornado.ioloop
import tornado.web
from controllers import index
from controllers import magnet
from controllers import admin
from controllers import e
from controllers import uimethods as umt
import os
import platform

if platform.system() == "Windows":
    import asyncio
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

BASE_DIR = os.path.dirname(__file__)

settings = {
    'template_path': os.path.join(BASE_DIR, 'template'),
    'static_path': os.path.join(BASE_DIR, 'static'),
    'static_url_prefix': '/static/',
    'cookie_secret': '43809138f51b96f8ac24e79b3a2cb482',
    'login_url': '/e/login',
    'xsrf_cookies': True,
    'debug': True,
    'autoreload': True,
    'ui_methods': umt,
}

application = tornado.web.Application([
    # 主页

    (r"/", index.IndexHandler),
    (r"/index", index.IndexHandler),
    # 文章页
    (r"/vod/([\d]+).html", index.VodHandler),
    # 查询
    (r"/search", index.SearchHandler),
    # 关于
    (r"/about", index.AboutHandler),

    # 登录
    (r"/e/login", e.LoginHandler),
    (r"/e/index", e.IndexHandler),
    (r"/e/welcome", e.WelcomeHandler),
    (r"/e/settings", e.SettingsHandler),
    (r"/e/vod", e.VodHandler),
    (r"/e/vodedit", e.VodeditHandler),
    (r"/e/userlist", e.UserlistHandler),
    (r"/e/profile", e.ProfileHandler),
    (r"/e/pwd", e.PwdHandler),
    # Dashboard
    (r"/e/article", e.ArticleHandler),
    (r"/e/vod", e.VodHandler),
    # Tag 管理
    # 个人资料管理
    # 友链管理
    (r"/e/flink", e.FlinkHandler),
    # 登出
    (r"/e/logout", e.LogoutHandler),
    # 出状态图
    (r"/e/api/v1/status", e.StatusApiHandler),

    # 登录
    (r"/admin/login", admin.LoginHandler),
    # Dashboard
    (r"/admin/index", admin.IndexHandler),
    # Dashboard
    (r"/admin/article", admin.ArticleHandler),
    (r"/admin/vod", admin.VodHandler),
    # Tag 管理
    (r"/admin/tag", admin.TagsHandler),
    # 个人资料管理
    (r"/admin/profile", admin.ProfileHandler),
    # 网站资料管理
    (r"/admin/blog", admin.BlogHandler),
    # 友链管理
    (r"/admin/flink", admin.FlinkHandler),
    # 登出
    (r"/admin/logout", admin.LogoutHandler),
    # 出状态图
    (r"/admin/api/v1/status", admin.StatusApiHandler),
    # 上传图片
    (r"/upload", admin.UploadHandler),
    # 文章页
    (r"/tag/([\d]+)", index.TagsHandler),
    # 404处理页面
    (r"/404", index.NotfindHandler),
    # 500处理页面
    (r"/500", index.ServerErrorHandler),
    (r".*", index.NotfindHandler),
], **settings)

if __name__ == '__main__':
    application.listen(8003)
    tornado.ioloop.IOLoop.instance().start()
